package com.shrct.businessprocess.money

import com.shrct.businessprocess.project.RCExchangeRate
import com.shrct.businessprocess.project.RCProjectMoney
import com.shrct.businessprocess.project.RCRealMoney

class Moneys {
	def static RCProjectMoney sum(Iterable<RCProjectMoney> aLotOfMoney, RCExchangeRate exchangeRate){
		var result = aLotOfMoney.reduce[m1,m2|m1.plus(m2)]
		if(result==null) new RCProjectMoney=>[ amount = 0d it.exchange = exchangeRate]
		else result.toMoney(exchangeRate)
	}
	def static RCRealMoney sumRealMoney(Iterable<RCRealMoney> aLotOfMoney, RCExchangeRate de){
		val result = aLotOfMoney.^map[toHostAmount].reduce[Double m1, Double m2|m1 + m2]
		if(result==null){
			new RCRealMoney()=>[
				it.amount = 0d;
				it.exchangeRate = 1d
				it.currencyType = de.currencyType
			]
		}else{
			new RCRealMoney()=>[
				it.amount = result / de.rate
				it.exchangeRate = 1d
				it.currencyType = de.currencyType
			]
		}
	}
	def static RCRealMoney sumHostRealMoney(Iterable<Double> aLotOfMoney, RCExchangeRate de){
		val result = aLotOfMoney.reduce[Double m1, Double m2|m1 + m2]
		if(result==null){
			new RCRealMoney()=>[
				it.amount = 0d;
				it.exchangeRate = 1d
				it.currencyType = de.currencyType
			]
		}else{
			new RCRealMoney()=>[
				it.amount = result / de.rate
				it.exchangeRate = 1d
				it.currencyType = de.currencyType
			]
		}
	}
	
	
}